﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public class KhachHang
    {
        
        public int MaKH { get; set; }

        public string MaKHHienThi
        {
            get
            {
                return "KH" + MaKH;
            }
        }
        public string TenKH { get; set; }
        public string DiaChi { get; set; }
        public string DienThoai { get; set; }
        public string Email { get; set; }
        public string Fax { get; set; }
        public string MaSoThue { get; set; }
        public int MaNV { get; set; }
        public string TenNV { get; set; }
        

        
        //load
        public KhachHang(int maKh, string tenKH, string diaChi, string dienThoai,
            string email, string fax, string maSoThue, int maNV, string tenNV)
        {
            this.MaKH = maKh;
            this.TenKH = tenKH;
            this.DiaChi = diaChi;
            this.DienThoai = dienThoai;
            this.Email = email;
            this.Fax = fax;
            this.MaSoThue = maSoThue;
            this.MaNV = maNV;
            this.TenNV = tenNV;
        }

        //them moi
        public KhachHang(string tenKH, string diaChi, string dienThoai,
           string email, string fax, string maSoThue, string maNV)
        {
            this.TenKH = tenKH;
            this.DiaChi = diaChi;
            this.DienThoai = dienThoai;
            this.Email = email;
            this.Fax = fax;
            this.MaSoThue = maSoThue;
            this.MaNV = Int16.Parse(maNV.Substring(2));
        }

        //cap nhat
        public KhachHang(string maKHHienThi, string tenKH, string diaChi, string dienThoai,
           string email, string fax, string maSoThue, string maNV)
        {
            this.MaKH = Int16.Parse(maKHHienThi.Substring(2));
            this.TenKH = tenKH;
            this.DiaChi = diaChi;
            this.DienThoai = dienThoai;
            this.Email = email;
            this.Fax = fax;
            this.MaSoThue = maSoThue;
            this.MaNV = Int16.Parse(maNV.Substring(2));
        }

        public static void ThemMoi(KhachHang nhapObject)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            DataAccess.KhachHang tamObject = new DataAccess.KhachHang();

            tamObject.TenKH = nhapObject.TenKH;
            tamObject.MaSoThue = nhapObject.MaSoThue;
            tamObject.Email = nhapObject.Email;
            tamObject.DiaChi = nhapObject.DiaChi;
            tamObject.DienThoai = nhapObject.DienThoai;
            tamObject.Fax = nhapObject.Fax;
            tamObject.MaNV = nhapObject.MaNV;

            db.KhachHangs.InsertOnSubmit(tamObject);
            db.SubmitChanges();
        }

        public static void CapNhat(KhachHang nhapObject)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            var tamObject = (from p in db.KhachHangs
                             where p.MaKH == nhapObject.MaKH
                             select p).Single();
            tamObject.TenKH = nhapObject.TenKH;
            tamObject.MaSoThue = nhapObject.MaSoThue;
            tamObject.Email = nhapObject.Email;
            tamObject.DiaChi = nhapObject.DiaChi;
            tamObject.DienThoai = nhapObject.DienThoai;
            tamObject.Fax = nhapObject.Fax;
            tamObject.MaNV = nhapObject.MaNV;
            db.SubmitChanges();
        }

        public static List<KhachHang> LayDanhSach()
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            return (from p in db.KhachHangs
                    select new KhachHang(p.MaKH, p.TenKH, p.DiaChi, p.DienThoai,
                        p.Email, p.Fax, p.MaSoThue, p.MaNV, p.NhanVien.TenNV)).ToList();
        }

        public static KhachHang LayThongTinChiTiet(int maKH)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            return (from p in db.KhachHangs
                    where p.MaKH == maKH
                    select new KhachHang(p.MaKH, p.TenKH, p.DiaChi, p.DienThoai,
                        p.Email, p.Fax, p.MaSoThue, p.MaNV, p.NhanVien.TenNV)).Single();
        }
        
    }
}
